// import { createRouter, createWebHistory } from "vue-router";
import { createRouter, createWebHashHistory } from "vue-router";
import ReceptionLayout from "@/layout/ReceptionLayout.vue";
import BackstageLayout from "@/layout/BackstageLayout.vue";
import HomeView from "../views/HomeView.vue";
import { Message } from "@arco-design/web-vue";
import { isLogin } from "./user";

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "home",
      component: HomeView,
    },
    {
      path: "/login",
      name: "login",
      component: () => import("@/views/Login/LoginView.vue"),
    },
    {
      path: "/reception",
      name: "reception",
      component: ReceptionLayout,
      children: [
        {
          path: "blogs",
          name: "blogs",
          component: () => import("@/views/Reception/BlogListView.vue"),
        },
        {
          path: "blogs/:id",
          name: "showBlog",
          component: () => import("@/views/Reception/BlogShowView.vue"),
        },
      ],
    },
    {
      path: "/backstage/admin",
      component: BackstageLayout,
      children: [
        {
          name: "backstageBlogs",
          path: "blogs",
          component: () => import("@/views/Backstage/BlogsListView.vue"),
        },
        {
          name: "backstageTags",
          path: "tags",
          component: () => import("@/views/Backstage/TagsListView.vue"),
        },
        {
          name: "backstageTagS1",
          path: "tags1",
          component: () => import("@/views/Backstage/TagsListView1.vue"),
        },
        {
          name: "backstageTagS2",
          path: "tags2",
          component: () => import("@/views/Backstage/TagsListView2.vue"),
        },
        {
          name: "blogcreate",
          path: "create",
          component: () => import("@/views/Backstage/BlogCreateView.vue"),
        },
        {
          name: "blogUpdate",
          path: "update/:id",
          component: () => import("@/views/Backstage/BlogCreateView.vue"),
        },
      ],
    },
    {
      path: "/backstage/admin1",
      component: BackstageLayout,
      children: [
        {
          name: "backstageTags2",
          path: "tags1",
          component: () => import("@/views/Backstage/TagsListView1.vue"),
        },
        {
          name: "backstageTags3",
          path: "tags2",
          component: () => import("@/views/Backstage/TagsListView2.vue"),
        },
      ],
    },
    {
      path: "/403",
      name: "403",
      component: () => import("../views/errors/PermissionDeny.vue"),
    },
    {
      path: "/:pathMatch(.*)*",
      name: "404",
      component: () => import("../views/errors/NotFount.vue"),
    },
  ],
});
router.beforeEach(async (to, from, next) => {
  if (isLogin()) {
    if (to.path.indexOf("/login") === 0) {
      next({
        name: from.name,
      });
    } else {
      next();
    }
  } else if (to.path.indexOf("/backstage") === 0) {
    // console.log(to);
    Message.error({
      content: "尚未登录，请登陆后访问！",
      closable: true,
      duration: 3000,
    });
    next({
      name: "login",
      query: {
        rewrite: to.name,
        ...to.query,
      },
      // state
    });
  } else {
    next();
  }
});

export default router;
